package com.ourteam.product.api;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractCRUDServiceController;
import com.ourteam.product.ui.dao.IProductUIFormFieldDAO;
import com.ourteam.product.ui.dao.ProductUIFormFieldDAOQueryBean;
import com.ourteam.product.ui.domain.ProductUIFormFieldBean;
import com.ourteam.product.ui.domain.ProductUIFormFieldTypeBean;
import com.ourteam.product.ui.service.IProductUIService;
import com.ourteam.product.ui.service.ProductUIServiceFactory;

@Controller
@RequestMapping("/api/PRODUCT/UI/productUIFormField")
public class ProductUIFormFieldAPI extends
		AbstractCRUDServiceController<ProductUIFormFieldDAOQueryBean, ProductUIFormFieldSearchModel, ProductUIFormFieldBean> {

	private static final IProductUIService PRODUCT_UI_SERVICE = ProductUIServiceFactory.getProductUIService();

	@Override
	protected ProductUIFormFieldDAOQueryBean newQueryBean() {
		return new ProductUIFormFieldDAOQueryBean();
	}

	@Override
	protected int queryCount(ProductUIFormFieldDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUIFormFieldCount(queryBean);
	}

	@Override
	protected ProductUIFormFieldBean[] queryData(ProductUIFormFieldDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUIFormFields(queryBean);
	}

	@Override
	protected void deleteData(long[] ids) throws Exception {
		PRODUCT_UI_SERVICE.deleteProductUIFormFields(ids);
	}

	@Override
	protected ProductUIFormFieldBean getDataById(long id) throws Exception {
		return PRODUCT_UI_SERVICE.getProductUIFormFieldById(id);
	}

	@Override
	protected void saveData(ProductUIFormFieldBean dataBean) throws Exception {
		if (dataBean.getId() == 0) {
			PRODUCT_UI_SERVICE.addNewProductUIFormField(dataBean);
		} else {
			PRODUCT_UI_SERVICE.modifyProductUIFormField(dataBean);
		}
	}

	@RequestMapping("/getFormFieldTypeList")
	@ResponseBody
	public ProductUIFormFieldTypeBean[] getFormFieldTypeList() throws Exception {

		return PRODUCT_UI_SERVICE.queryProductUIFormFieldTypes(null);
	}

	@RequestMapping("/getFormGroupList")
	@ResponseBody
	public ProductUIFormFieldBean[] getFormGroupList(@RequestParam long formId) throws Exception {

		ProductUIFormFieldDAOQueryBean queryBean = new ProductUIFormFieldDAOQueryBean();

		queryBean.createCriteria().andFormIdEqualTo(formId).andParentIdEqualTo(0).andTypeWidgetEqualTo("group");

		queryBean.setSortProperty(IProductUIFormFieldDAO.SortIndex);

		return PRODUCT_UI_SERVICE.queryProductUIFormFields(queryBean);
	}

}
